#!/usr/bin/env python3
###############################################
# TODO: Argparse Support                      #
###############################################
import datetime;
import sys;
import os;
import re;
import pwd;
import subprocess;
# Parse out the terminal size 
def terminal_size():
    import fcntl, termios, struct
    th, tw, hp, wp = struct.unpack('HHHH',
        fcntl.ioctl(0, termios.TIOCGWINSZ,
        struct.pack('HHHH', 0, 0, 0, 0)));
    return int(tw*0.8);


time = datetime.datetime.now().time();

hashes= '#' * terminal_size();

blue=("0","0","255");

name= "Claire";
ansi_escape = re.compile(r'\x1b[^m]*m')
# Definition for the hash printing functions
def find_cpu():
    with open('/proc/cpuinfo', 'r') as inF:
        for line in inF:
            if 'model name' in line:
                return re.sub( '\s+', ' ', line[(line.index(": ")+2):].rstrip().replace("(R)","®").replace("(TM)","™") ).strip();
                break;

def divideInHalf(str):
    return str[:round(len(str)/2)], str[round(len(str))/2:]

def print_color(r,g,b):
    return "\x1b[38;2;"+r+";"+g+";"+b+"m";

def print_initial_last():
    print(print_color(blue[0],blue[1],blue[2]) + hashes + u"\u001b[0m");

def print_nextline(toprint):
    print(print_color(blue[0],blue[1],blue[2]) + '# ' + toprint + (' ' * (terminal_size() - len(ansi_escape.sub('',toprint)) -3 ) + print_color("0","0","255") + '#' +  u"\u001b[0m"));

def print_in_hashes(toprint):
    print_lines=toprint.splitlines();
    print_initial_last();
    for i in range(0, len(print_lines)):
        if len(ansi_escape.sub("",print_lines[i])) > (terminal_size()-3):
            print_nextline(print_lines[i][:len(ansi_escape.sub('',print_lines[i]))]);
            print_nextline(print_lines[i][len(ansi_escape.sub('',print_lines[i])):]);
            
        else:
            print_nextline(print_lines[i]);
            
    print_initial_last();

def get_uname():
    return pwd.getpwuid( os.getuid() )[ 0];

# Parsing out command arguments (SWITCH THIS TO ARGPARSE SOON 
if len(sys.argv) > 1:
    if "12" in sys.argv:
        time = time.strftime("%-I:%M %p");
else:
    time= time.strftime("%-H:%M");

shell = os.readlink('/proc/%d/exe' % os.getppid());
# merge this into one statement when you get some time

distro =(re.sub(r"\n"," ", (subprocess.run(["lsb_release","-sirs"], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('utf-8'))));
cpu = find_cpu()
#This uses ANSI color codes, with u"\u001b[0m" resetting the colors.
#\u001b[41m specifcies a background, and the three part one specifies a foreground.
message= (u"\u001b[38;5;14m" + "Welcome To "+ name + "'s Terminal!\n" +
          
u"\u001b[38;5;10m" + "The time is: "   +  u"\u001b[38;5;14m" + u"\u001b[41m" + time + u"\u001b[0m" +
          
          "\n" + print_color("51","255","249") + "Shell: " + shell + u"\u001b[0m" +
          
          "\n"+ print_color("172","229","238") + "Username: " + get_uname() + u"\u001b[0m" +
          
          "\n"+ print_color("128","255","0") + "Distro: " + distro  + u"\u001b[0m" +
          "\n"+ print_color("51","255","249") + "Python Version: " + sys.version[0:(sys.version.index(" "))] + u"\u001b[0m" +
          
          "\n"+ print_color("0","255","0")+ "Processor: " + cpu
);

print_in_hashes(message);
